package com.fjec.common.shiro;

import com.fjec.common.shiro.feign.PermissionService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

//@Configuration
@EnableFeignClients
public class CommonRealm extends AuthorizingRealm {

    @Autowired
    private PermissionService permissionService;

    public CommonRealm() {
//        this.permissionService = permissionService;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        List<String> roleList = AccountInfo.getRole();
        Set<String> permissions = new HashSet<>();
        roleList.forEach(role -> permissions.addAll(permissionService.getPermissions(role)));

        simpleAuthorizationInfo.setStringPermissions(permissions);
        return simpleAuthorizationInfo;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        return null;
    }
}
